package net.result;

import com.opensymphony.xwork2.ActionInvocation;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;
import net.base.ToolsAction;
import org.apache.struts2.ServletActionContext;

import javax.servlet.ServletContext;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Map;

public class XmlResult extends BaseResult {


    protected void doExecute(String arg0, ActionInvocation invocation) throws Exception {
        // TODO Auto-generated method stub
        // TODO Auto-generated method stub
        ServletContext sc = ServletActionContext.getServletContext();
        HttpServletRequest request = ServletActionContext.getRequest();
        HttpServletResponse response = ServletActionContext.getResponse();
        // response.setCharacterEncoding(response.getCharacterEncoding());
        response.setContentType("text/html;charset=UTF-8");


        ToolsAction baseAction = (ToolsAction) invocation.getAction();
        ServletOutputStream output = response.getOutputStream();


        XStream xstream = new XStream(new DomDriver());
        xstream.alias("item", Map.class);
        xstream.alias("items", ArrayList.class);
        xstream.registerConverter(new net.result.MapConverter(xstream.getMapper()));
        String result = xstream.toXML(baseAction.getModel());
        xstream.toXML(baseAction.getModel(), output);
        System.out.println(result);


    }

}
